package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService UserInfoService;

    /**
     * 查询用户基础信息
     *
     * @param userID
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID, @RequestHeader("Authorization") String token) {


        //3,判断用户id
        if (userID == null) {
            userID = Long.valueOf(UserHolder.getUserId());
        }

        UserInfoVo userInfo = UserInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {


        //设置id
        userInfo.setId(UserHolder.getUserId());
        UserInfoService.update(userInfo);
        return ResponseEntity.ok(null);

    }

    /**
     * 更新头像
     *
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateUserHead(MultipartFile headPhoto) throws IOException {
        UserInfoService.updateUserHead(headPhoto);
        return ResponseEntity.ok(null);
    }

    /**
     *统计好友,喜欢,粉丝数量
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        CountsVo vo = UserInfoService.counts();
        return ResponseEntity.ok(vo);
    }
}